/*
 * Bunisoft the Open Source Communications Company
 * Copyright 2006, Bunisoft Inc.,
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.teremail.panto;

import java.io.IOException;
import java.io.InputStream;


/**
 * Listens to events generated by the parser.  The user should implement
 * this interface in order to handle the parsing of an email message.
 * 
 * @author Michael Barker
 *
 */
public interface ContentHandler {

    /**
     * Called at the beginning of a message.
     *
     */
    void startMessage();
    /**
     * Called at the end of a message.
     *
     */
    void endMessage();
    
    /**
     * Called at the start of a body part.
     */
    void startBodyPart();
    
    /**
     * Called atht end of a body part
     */
    void endBodyPart();
    
    /**
     * Called at the beginning of a (mime or message) header block.
     *
     */
    void startHeader();
    
    /**
     * Called for each field of a header.
     * 
     * @param fieldData the raw contents of the field 
     *        (<code>Field-Name: field value</code>). The value will not be 
     *        unfolded.
     */
    void field(String fieldData);
    
    /**
     * Called at the end of a header block.
     *
     */
    void endHeader();
    
    /**
     * The small chunk of information at the beginning of a multipart
     * messsage.
     * 
     * @param in Contains the data.  The user MUST read to the end
     * of this stream.
     */
    void preamble(InputStream in) throws IOException;
    
    /**
     * The small chunk of information at the end of a multipart
     * messsage.
     * 
     * @param in Contains the data.  The user MUST read to the end
     * of this stream.
     */    
    void epilogue(InputStream in) throws IOException;

    /**
     * Called at the start of a multipart entity.
     * 
     */
    void startMultipart(BodyHeader header);
    
    /**
     * Called at the start of a multipart entity.
     * 
     */
    void endMultipart();
    
    /**
     * Called when the body of a discrete (non-multipart) entity is about to 
     * be parsed.
     * 
     * @param is the contents of the body.  The user MUST read to the end
     *  of this stream. 
     * @throws IOException should be thrown on I/O errors.
     */
    void body(BodyHeader header, InputStream is) throws IOException;
    
}
